//
//  ShangPinXiangQingViewController.m
//  EJLX
//
//  Created by adc on 15/11/23.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "ShangPinXiangQingViewController.h"
#import "HttpsManager.h"


#import "FirstJiaRuTableViewCell.h"

#import "ShangPinFeNLeiShuXing.h"
#import "ChildrenAttr.h"
#import "TwoShangPinXiangQingTableViewCell.h"
#import "ThireShangPinXiangQingTableViewCell.h"
#import "FourShangPinXiangQingTableViewCell.h"
#import "QueRenDingDanViewController.h"
#import "MyShopVC.h"
#import "jiaRuGouWuChe.h"
#import "ShoppingOfCartVC.h"
#import "ShangPinModel.h"
#import "GoodsinfoTableView.h"
@interface ShangPinXiangQingViewController ()

@end

@implementation ShangPinXiangQingViewController
-(void)viewWillAppear:(BOOL)animated
{
    [tabelV removeFromSuperview];
    [GMDCircleLoader setOnView:self.view withTitle:@"..." animated:YES];
    self.automaticallyAdjustsScrollViewInsets = NO ;
    _color=NO;
    self.view.backgroundColor=[UIColor whiteColor];
    // _a=0;
    NSLog(@"zzz%@",_productId);
    
    [HttpsManager requestToGetShangPinXiangQingCompletion:_productId com:^(ShangPinXiangQing *shangPin)
     {
         if (shangPin!=nil)
         {
             self.shangP=shangPin;
             [self zz];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
     }];
    
    [GMDCircleLoader hideFromView:self.view animated:YES];
}
-(void)viewDidAppear:(BOOL)animated
{
    self.view.backgroundColor=[UIColor whiteColor];
    _bigScr.contentSize=CGSizeMake(_bigScr.frame.size.width,HEIGHT*2);
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  [GMDCircleLoader setOnView:self.view withTitle:@"..." animated:YES];
     self.automaticallyAdjustsScrollViewInsets = NO ;
    _color=NO;
    self.view.backgroundColor=[UIColor whiteColor];
    _a=1;
    NSLog(@"zzz%@",_productId);
    
    [HttpsManager requestToGetShangPinXiangQingCompletion:_productId com:^(ShangPinXiangQing *shangPin)
     {
         if (shangPin!=nil)
         {
             self.shangP=shangPin;
             [self zz];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
     }];
    
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    
    [self.view addSubview:view];

    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-55, 10,110, 24)];
    label.text=@"商品详情";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
//    UIView*bigView=[[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-60, WIDE, 60)];
//    [self.view addSubview:bigView];
//    UILabel*l=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
//    l.backgroundColor=[UIColor redColor];
//    [bigView addSubview:l];
    for (int i=0;i<3; i++)
    {
        UIButton*smellBut=[[UIButton alloc]initWithFrame:CGRectMake(WIDE/6*i,0,WIDE/6, _bigView.frame.size.height)];
        smellBut.tag=i;
        NSArray*ary=[NSArray arrayWithObjects:@"客服1",@"进店",@"收藏1", nil];
        UIImage*str=[UIImage imageNamed:ary[i]];
        [smellBut setImage:str forState:UIControlStateNormal];
        [smellBut addTarget:self action:@selector(smellBut:) forControlEvents:UIControlEventTouchUpInside];
        [_bigView addSubview:smellBut];
    }
    for (int a=0;a<2;a++)
    {
        UIButton*bigBut=[[UIButton alloc]initWithFrame:CGRectMake(WIDE/2+WIDE/4*a,0 ,WIDE/2/2,  _bigView.frame.size.height)];
        bigBut.tag=a;
        NSArray*ary=[NSArray arrayWithObjects:@"加入购物车1.jpg",@"立即购买1.jpg", nil];
        UIImage*str=[UIImage imageNamed:ary[a]];
        [bigBut setImage:str forState:UIControlStateNormal];
        [bigBut addTarget:self action:@selector(bigBut:) forControlEvents:UIControlEventTouchUpInside];
        [_bigView addSubview:bigBut];
    }
   

    _web.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _web.backgroundColor = [UIColor lightGrayColor];
    _web.scalesPageToFit = YES;
    _web.contentMode = UIViewContentModeRedraw;
    _web.opaque = YES;
    [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.nuomi.com"]]];
      [GMDCircleLoader hideFromView:self.view animated:YES];
    
       }




-(void)bigBut:(UIButton*)but
{
  //  if (but.tag == 0) {
        [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
        GoodsinfoTableView *table = [[GoodsinfoTableView alloc] init];
        table.productId = _productId;
        NSLog(@"tab %@]]]] %d",_productId,_shangP.discountPrice);
        // table.detail = self.detail;
        table.shopID = _shopId;
        table.price =  [NSString stringWithFormat:@"手机专享价:%.2f" ,_shangP.discountPrice*0.01];
        table.num = [NSString stringWithFormat:@"库存：%d", _shangP.collectionNum];
        self.definesPresentationContext = YES;
        table.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        table.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:table animated:YES completion:nil];
        [GMDCircleLoader hideFromView:self.view animated:YES];
    
    
        table.yanAndChiBlock =^(NSString *str,NSString *yanse,NSString *chima,NSString *num,NSString *attr1,NSString*attr2){
          
                if (but.tag == 0) {
                      if ([str isEqualToString:@"OK"]) {
                    ShoppingOfCartVC *car =[[ShoppingOfCartVC alloc] init];
                    [self.navigationController pushViewController:car animated:YES];
                      }
                }else{
                    QueRenDingDanViewController*vcz=[[QueRenDingDanViewController alloc]init];
                    ShangPinModel*vc=[[ShangPinModel alloc]init];
                    vc.jiaGe=_shouJia.text;
                    vc.name=_name.text;
                    vc.shuLiang=shuLiangLabel.text;
                    vc.attr1Id=_attr1Id;
                        vc.attr2Id=_attr2Id;
                        vc.shuLiang= num;
                        vc.yanSe= yanse;
                        vc.chiMa= chima;
                   
                    vc.productId=_productId;
                    vc.shopId=_shopId;
                    
                    vcz.a=1;
                 
                    vcz.shangPinAry=[NSMutableArray array];
                    [vcz.shangPinAry addObject:vc];
                    
                    [self.navigationController pushViewController:vcz animated:YES];
                }
       
        };

   // }
    
}

-(void)smellBut:(UIButton*)sender
{
    if (sender.tag==0)
    {
        NSString *number = @"13156508314";// 此处读入电话号码
        // NSString *num = [[NSString alloc]initWithFormat:@"tel://%@",number]; //number为号码字符串 如果使用这个方法结束电话之后会进入联系人列表
        
        NSString *num = [[NSString alloc]initWithFormat:@"telprompt://%@",number]; //而这个方法则打电话前先弹框 是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
        //拨号
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:num]];
    }
    if (sender.tag==1)
    {
        MyShopVC*vc=[[MyShopVC alloc]init];
        vc.shopId=[NSString stringWithFormat:@"%d",_shangP.shopId];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (sender.tag==2)
    {
        [HttpsManager requestGetshouCang:[NSString stringWithFormat:@"%d",_shangP.ID] shop:[NSString stringWithFormat:@"%d",_shangP.shopId] Competion:^(int errcode, NSString *errmsg)
        
        {
            SHOW_ALERT(errmsg);
            
            
            
        }];
    }
}
-(void)changeTuPian
{
    [_smellScr setContentOffset:CGPointMake(page.currentPage*WIDE, 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    page.currentPage = scrollView.contentOffset.x/ WIDE;
}
-(void)zz
{
    NSArray*dataAry=_shangP.resuleAry;
    _smellScr.contentSize=CGSizeMake(WIDE*dataAry.count, self.smellScr.frame.size.height);
    
    for (int i=0; i<dataAry.count; i++)
    {
        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.smellScr.frame.size.width*i,5, self.smellScr.frame.size.width, self.smellScr.frame.size.height-5)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:dataAry[i]] placeholderImage:[UIImage imageNamed:@"青色.jpg"]];
        
        [_smellScr addSubview:imageView];
    }
    _smellScr.bounces=NO;
    _smellScr.pagingEnabled=YES;
    _smellScr.showsHorizontalScrollIndicator=NO;
    //_smellScr.delegate=self;
    page=[[UIPageControl alloc]initWithFrame:CGRectMake(WIDE/2-30,110, 60, 20)];
    //page.backgroundColor=[UIColor redColor];
    page.numberOfPages=dataAry.count;
    page.pageIndicatorTintColor=[UIColor blackColor];
    page.currentPageIndicatorTintColor=[UIColor greenColor];
    [page addTarget:self action:@selector(changeTuPian) forControlEvents:UIControlEventValueChanged];
    [_bigScr addSubview:page];
    _name.text=_shangP.title;
    _yongJin.text=[NSString stringWithFormat:@"%.2f",_shangP.commision*0.01];
    _shouJia.text=[NSString stringWithFormat:@"%.2f",_shangP.discountPrice*0.01];
    _zheKou.text=_shangP.discount;
    float zhe=(float)_shangP.discountPrice/_shangP.price;
    _zheKou.text=[NSString stringWithFormat:@"%.1f折",zhe*10];
    _youFei.text=[NSString stringWithFormat:@"%.2f",_shangP.stockNum*0.01];
    _shiJia.text=[NSString stringWithFormat:@"%.2f",_shangP.price*0.01];
    NSString*str3=[NSString stringWithFormat:@"%d",_shangP.collectionNum];
    _shouCang.text=str3;

}
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


